﻿Imports System.Data.SqlClient

Public Class 新建计划
    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles 开始时间_data.ValueChanged

    End Sub

    Private Sub 退出btn_Click(sender As Object, e As EventArgs) Handles 退出btn.Click
        Me.Close()
    End Sub

    Private Sub 保存btn_Click(sender As Object, e As EventArgs) Handles 保存btn.Click
        ' ==== 参数校验 ====
        If String.IsNullOrWhiteSpace(计划标题_data.Text) Then
            MessageBox.Show("计划标题不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            计划标题_data.Focus()
            Exit Sub
        End If

        If String.IsNullOrWhiteSpace(计划内容_data.Text) Then
            MessageBox.Show("计划内容不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            计划内容_data.Focus()
            Exit Sub
        End If

        If 结束时间_data.Value < 开始时间_data.Value Then
            MessageBox.Show("结束时间不能早于开始时间！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        Dim returnCode As Integer = -1

        Try
            Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
                Using cmd As New SqlCommand("usp_AddNewJL", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    ' 传参
                    cmd.Parameters.AddWithValue("@JHBT", 计划标题_data.Text.Trim())
                    cmd.Parameters.AddWithValue("@JHNR", 计划内容_data.Text.Trim())
                    cmd.Parameters.AddWithValue("@ZYCD", CInt(重要程度_data.Value))
                    cmd.Parameters.AddWithValue("@KSSJ", 开始时间_data.Value)
                    cmd.Parameters.AddWithValue("@JSSJ", 结束时间_data.Value)

                    ' 定义返回值参数
                    Dim retParam As New SqlParameter("@RETURN_VALUE", SqlDbType.Int)
                    retParam.Direction = ParameterDirection.ReturnValue
                    cmd.Parameters.Add(retParam)

                    conn.Open()
                    cmd.ExecuteNonQuery()
                    returnCode = CInt(retParam.Value)
                End Using
            End Using

            ' ==== 根据返回值判断 ====
            Select Case returnCode
                Case 0
                    MessageBox.Show("✅ 计划保存成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
                   ' Me.Close()

                Case 1
                    MessageBox.Show("⚠️ 已存在相同标题与时间段的计划，无法重复添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)

                Case 9
                    MessageBox.Show("❌ 保存时发生未知错误，请联系管理员。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)

                Case Else
                    MessageBox.Show("⚠️ 返回未知代码：" & returnCode.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End Select

        Catch ex As Exception
            MessageBox.Show("❌ 保存失败：" & ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub




    ' ========== 自动计算计划天数 ==========
    Private Sub 时间控件_ValueChanged(sender As Object, e As EventArgs) Handles 开始时间_data.ValueChanged, 结束时间_data.ValueChanged
        If 结束时间_data.Value >= 开始时间_data.Value Then
            Dim days As Integer = (结束时间_data.Value - 开始时间_data.Value).Days + 1
            计划天数_data.Text = days.ToString()
        Else
            计划天数_data.Text = "0"
        End If
    End Sub
End Class